package org.example.service.post.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.annotation.LogTrace;
import org.example.dao.dao.post.PCommentDao;
import org.example.dao.dao.post.PReplyDao;
import org.example.dao.dao.user.UUserDao;
import org.example.dao.dto.commom.UserInfo;
import org.example.dao.dto.post.PReplyDto;
import org.example.dao.dto.user.UserDto;
import org.example.dao.entity.post.PComment;
import org.example.dao.entity.post.PReply;
import org.example.dao.entity.user.UUser;
import org.example.exception.ParamLossErrorException;
import org.example.service.post.ReplyService;
import org.example.skytool.commom.ContextUtil;
import org.example.skytool.commom.IdWorker;
import org.example.skytool.redis.RedisUtils;
import org.example.utils.post.reply.ExtendReplyService;
import org.example.utils.post.reply.RedisReplyService;
import org.example.utils.user.RedisUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 22449
 */
@Service("replyService")
public class ReplyServiceImpl implements ReplyService {

    @Resource
    private ContextUtil contextUtil;

    @Resource
    private ExtendReplyService extendReplyService;

    @Resource
    private RedisReplyService redisReplyService;

    @Resource
    private RedisUserService redisUserService;

    @Resource
    private IdWorker idWorker;

    @Resource
    private PCommentDao pCommentDao;

    @Resource
    private RedisUtils redisUtils;

    @Resource
    private PReplyDao pReplyDao;

    @Resource
    private UUserDao uUserDao;

    @Override
    @LogTrace
    public PReplyDto replyComment(Long receiverId, Long commentId, String content) {
        Long receiverUserId = receiverId;
        Long snederUserId = contextUtil.getId();
        Long pCommentId = (commentId);
        UUser receiverUser = uUserDao.selectById(receiverUserId);
        PComment pComment = pCommentDao.selectById(pCommentId);
        if (receiverUser == null) {
            throw new ParamLossErrorException("不存在被回复者用户,id为" + receiverId.toString());
        }
        if (pComment == null) {
            throw new ParamLossErrorException("不存在该评论,id为" + commentId.toString());
        }

        Long id = idWorker.nextId();
        PReply pReply = new PReply(id, snederUserId, receiverUserId, pCommentId, content);
        pReplyDao.insert(pReply);
        PReplyDto pReplyDto = new PReplyDto();
        pReplyDto.setPReply(pReply);
        UserDto userDto = contextUtil.getLoginUser();
        pReplyDto.setReceiverUser(new UserInfo(receiverUser));
        pReplyDto.setSenderUser(new UserInfo(userDto));

        //加入缓存队列
        redisUserService.addUserReplyReadList(receiverId.toString(), pReply.getId().toString());
        redisUserService.addUserReplyNoReadList(receiverId.toString(), pReply.getId().toString());
        return pReplyDto;
    }


    @Override
    @LogTrace
    public IPage<PReplyDto> getReplyByComment(Long commentId, Integer limit, Integer start) {
        LambdaQueryWrapper<PReply> lambdaQueryWrapper = extendReplyService.getLambdaQueryWrapper();
        lambdaQueryWrapper.eq(PReply::getCommentId, commentId);
        IPage<PReply> pPosts = pReplyDao.selectPage(new Page<>(start, limit), lambdaQueryWrapper);
        return extendReplyService.getReplyDtoPage(pPosts);
    }


    @Override
    @LogTrace
    public Long likeReply(Long id) {
        return redisReplyService.likeReply(id.toString());
    }

    @Override
    @LogTrace
    public void deleteReply(Long id) {
        PReply pReply = pReplyDao.selectById(id);
        pReply.setUpdateTime(new Date());
        pReply.setIsValid(0);
        pReplyDao.updateById(pReply);
    }

    @Override
    @LogTrace
    public PReplyDto updateReply(Long id, String content) {
        PReply pReply = pReplyDao.selectById(id);
        pReply.setUpdateTime(new Date());
        pReply.setContent(content);
        pReplyDao.updateById(pReply);
        PReplyDto pReplyDto = new PReplyDto();
        pReplyDto.setPReply(pReply);
        UserDto userDto = contextUtil.getLoginUser();
        UUser receiverUser = uUserDao.selectById(pReply.getReceiverId());
        pReplyDto.setReceiverUser(new UserInfo(receiverUser));
        pReplyDto.setSenderUser(new UserInfo(userDto));
        return pReplyDto;
    }

    @Override
    @LogTrace
    public IPage<PReplyDto> getReplyByMine(Long limit, Long start) {
        LambdaQueryWrapper<PReply> lambdaQueryWrapper = extendReplyService.getLambdaQueryWrapper();
        Long userId = contextUtil.getId();
        lambdaQueryWrapper.eq(PReply::getSenderId, (userId));
        IPage<PReply> pPosts = pReplyDao.selectPage(new Page<>(start, limit), lambdaQueryWrapper);
        return extendReplyService.getReplyDtoPage(pPosts);
    }


    @Override
    @LogTrace
    public List<PReplyDto> getReplyByToMine(Integer isread, Long limit, Long start) {
        List<String> set = redisUserService.getReplyByToMine(isread,limit,start);
        if(set==null||set.size()==0){
            return null;
        }
        LambdaQueryWrapper<PReply> lambdaQueryWrapper = extendReplyService.getLambdaQueryWrapper();
        lambdaQueryWrapper.in(PReply::getId, set);
        List<PReply> pPosts = pReplyDao.selectList(lambdaQueryWrapper);
        List<PReplyDto> list = new ArrayList<>();
        for (PReply post : pPosts) {
            PReplyDto postDto = extendReplyService.createReplyDto(post);
            list.add(postDto);
        }
        return list;
    }


}
